<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自动关闭弹框示例</title>
    <style>

        .modal {
            display: none; 
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgb(0,0,0);
            background-color: rgba(0,0,0,0.4);
        }


        .modal-content {
            background-color: #fefefe;
            margin: 15% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 80%;
        }


        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }


        .close:hover,


        .close:focus {
            color: black;
            text-decoration: none;
            cursor: pointer;
        }
    </style>
</head>


<body>
    <div id="myModal" class="modal">
        <div class="modal-content">
            <span class="close" id="closeBtn">&times;</span>
            <p>这是一个弹框,将在3秒后自动关闭。</p>
        </div>
    </div>


<script>
    var modal = document.getElementById("myModal");
    var closeBtn = document.getElementById("closeBtn");
    function showModal() {
        modal.style.display = "block";
        setTimeout(closeModal, 3000);
    }

    function closeModal() {
        modal.style.display = "none";
    }

    closeBtn.onclick = function() {
        closeModal();
    }

    window.onload = function() {
        showModal();
    }
</script>
</body>
</html>